package com.tarena.lbs.content.service.impl;

import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.po.ArticleSearchPO;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ElasticsearchOperations elasticsearchOperations;

    public PageResult<ArticleVO> pageList(ArticleQuery articleQuery) {
        List<ArticleSearchPO> articles = searchArticles(articleQuery);
        long total = articles.size();
        List<ArticleVO> articleVOs = articles.stream().map(this::convertToArticleVO).collect(Collectors.toList());
        PageResult<ArticleVO> pageResult = new PageResult<>();
        pageResult.setObjects(articleVOs);
        pageResult.setTotal(total);
        pageResult.setPageNo(articleQuery.getPageNo());
        pageResult.setPageSize(articleQuery.getPageSize());
        return pageResult;
    }

    public List<ArticleSearchPO> searchArticles(ArticleQuery articleQuery) {
        // 构建布尔查询，用于组合多个查询条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        // 根据查询条件添加查询子句
        if (articleQuery.getArticleTitle() != null && !articleQuery.getArticleTitle().isEmpty()) {
            boolQuery.must(QueryBuilders.matchQuery("articleTitle", articleQuery.getArticleTitle()));
        }
        if (articleQuery.getArticleCategoryId() != null) {
            boolQuery.must(QueryBuilders.termQuery("articleCategoryId", articleQuery.getArticleCategoryId()));
        }
        if (!articleQuery.getArticleStatus().isEmpty()) {
            boolQuery.must(QueryBuilders.termsQuery("articleStatus", articleQuery.getArticleStatus()));
        }
        if (articleQuery.getActivityId() != null) {
            boolQuery.must(QueryBuilders.termQuery("activityId", articleQuery.getActivityId()));
        }
        // 可以根据需要添加更多查询条件

        // 构建查询对象
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQuery)
                .withPageable(PageRequest.of(articleQuery.getPageNo() - 1, articleQuery.getPageSize()))
                .build();

        // 执行查询
        SearchHits<ArticleSearchPO> searchHits = elasticsearchOperations.search(searchQuery, ArticleSearchPO.class);

        // 处理查询结果
        List<ArticleSearchPO> articles = new ArrayList<>();
        for (SearchHit<ArticleSearchPO> hit : searchHits) {
            articles.add(hit.getContent());
        }
        return articles;
    }

    private ArticleVO convertToArticleVO(ArticleSearchPO articleSearchPO) {
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(articleSearchPO, articleVO);
        return articleVO;
    }
}